package com.github.edu.repair.manager.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.github.admin.edu.assembly.common.entity.JsonEntity;
import com.github.admin.edu.assembly.common.util.JsonUtils;
import com.github.admin.edu.assembly.common.util.ObjectUtil;
import com.github.admin.edu.assembly.common.util.UUIDUtils;
import com.github.admin.edu.assembly.string.util.StringUtils;
import com.github.admin.edu.orm.serivce.impl.DataRestfulAbstractBaseService;
import com.github.edu.repair.manager.domain.SysStepServerDomain;
import com.github.edu.repair.manager.domain.SysStepWfServerDomain;
import com.github.edu.repair.manager.entity.TSysStep;
import com.github.edu.repair.manager.entity.TSysWfStep;
import com.github.edu.repair.manager.service.SysStepManagerRestService;
import com.github.edu.repair.manager.service.SysStepWfManagerRestService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.util.List;

/**
 * Create by IntelliJ IDEA
 * 用户：王建
 * 日期：2018-8-29
 */
@Slf4j
@Service
public class SysStepWfManagerRestServiceImpl extends DataRestfulAbstractBaseService<TSysWfStep,String>
        implements SysStepWfManagerRestService {

    @Autowired
    private SysStepWfServerDomain domain;

    @Override
    public String getAllGetEntity(String title, Integer current, Integer rows, String order, String fileId) {
        TSysWfStep tSysUser=new TSysWfStep();
        if(StringUtils.isNotBlank(title)){
            tSysUser.setName(title);
        }
        return super.getAllGetEntity(tSysUser,current,rows,order,fileId);
    }

    @Override
    public String getAllEntity(String title, String order, String fileId) {
        return getAllGetEntity(title,0,0,order,fileId);
    }

    @Transactional
    @Override
    public String saveOrUpdate(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TSysWfStep> tJsonEntity= JsonUtils.toCollection(jsonEntity, new TypeReference<JsonEntity<TSysWfStep>>() {
            });
            TSysWfStep t=tJsonEntity.getData();
            Object o= ObjectUtil.getValueByKey(t,"id");
            if(org.springframework.util.StringUtils.isEmpty(o)){
                t.setId(UUIDUtils.generateCode());
            }
            t=domain.save(t);
            tJsonEntity.setData(t);
            return JsonUtils.toJson(tJsonEntity);
        }
        return JsonUtils.toJson(new JsonEntity<TSysWfStep>("保存对象为空！"));
    }

    @Transactional
    @Override
    public String delete(String ids) {
        return super.delete(ids,"String");
    }

    @Override
    public String getALLPostByPager(String jsonEntity) {
        if(StringUtils.isNotBlank(jsonEntity)){
            JsonEntity<TSysWfStep> tJsonEntity= JsonUtils.toCollection(jsonEntity, new TypeReference<JsonEntity<TSysWfStep>>() {
            });
            if(null!=tJsonEntity){
                return super.getALLPostByPager(tJsonEntity.getData(),tJsonEntity.getPager());
            }
        }
        return JsonUtils.toJson(new JsonEntity<TSysWfStep>("未找到参数！"));
    }

    @Transactional(readOnly = true)
    @Override
    public String queryStepEntity(Integer stepId, String lcbh) {

        JsonEntity<TSysWfStep> jsonEntity=new JsonEntity<>();

        if(StringUtils.isNotBlank(lcbh)&&null!=stepId){
            List<TSysWfStep> list=domain.getAllByStepAndLcbh(stepId,lcbh);
            if(null!=list&&list.size()>0){
                jsonEntity.setData(list.get(0));
            }
        }
        return JsonUtils.toJson(jsonEntity);
    }
}
